package com.example.mongodbdemo.controller;

import com.example.mongodbdemo.domin.Comment;
import com.example.mongodbdemo.dto.R;
import com.example.mongodbdemo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/pinglun")
public class MongoDBcontroller {
    @Autowired
    private CommentService commentService;
    @PutMapping("/save")
    public R saveOne(@RequestBody Comment p){
        commentService.savePinglun(p);
        return new R(true);
    }
    @PostMapping("/update")
    public R updateOne(@RequestBody Comment p){
        commentService.updatePinglun(p);
        return new R(true);
    }
    @DeleteMapping("/{id}")
    public R deleteByid(@PathVariable int id){
        commentService.deletePinglunById(String.valueOf(id));
        return new R(true);
    }
    @GetMapping
    public R getAll(){
        return new R(true,commentService.findPinglunList());
    }
    @GetMapping("/{id}")
    public R getByid(@PathVariable int id){
        return new R(true,commentService.findPinglunById(String.valueOf(id)));
    }
    @GetMapping("/findPpage")
    public R findPpage(@RequestParam int parentid,
                       @RequestParam int pageNum,
                       @RequestParam int pageSize){
        return new R(true,commentService.findPinglunByparentid(String.valueOf(parentid),pageNum,pageSize));
    }
//    @GetMapping("/findPage")
//    public R findPage(@RequestParam int pageNum,
//                       @RequestParam int pageSize){
//        return new R(true,commentService.findPage(pageNum,pageSize));
//    }
    @PostMapping("/likeadd")
    public R likeAdd(@RequestParam int id){
        commentService.updateLikeNum(String.valueOf(id));
        return new R(true);
    }
}
